<?php

namespace Common\Logic;

/**
 * 房型逻辑
 * @modify trainyao 2016-01-04 创建
 */
class RoomTypeLogic extends \Common\Controller\BaseController {
    //@modify zhangzhen 2017-01-10 添加rf卡标识
    private $typeMap = array(
        'mz' => 0,
        'qrm' => 1,
        'rf' => 2,
        'rfAndQrm' => 3,
        'other' => 4,
    );

    private $priceLogMap = array(
        'zh_cn' => array(
            'discount' => '设置折扣',
            'price' => '价格由',
            'change' => '改为',
            'set' => '价格设置为',
        ),
        'en_us' => array(
            'discount' => 'setting discount',
            'price' => 'price from ',
            'change' => ' to ',
            'set' => 'set price ',
        ),
        'zh_tw' => array(
            'discount' => '設置折扣',
            'price' => '價格由',
            'change' => '改為',
            'set' => '價格設置为',
        ),
    );
    /**
     * 添加房型
     * @modify trainyao 2016-01-04     创建
     * @param  integer   $hotelId       客栈id
     * @param  string   $roomTypeName  房型名称
     * @param  string   $roomTypePrice 房型默认价格
     * @param  string   $roomTypeCharityPrice 房型默认义卖价格
     * @param  array    $roomData      房间数据
     * @return boolean 执行结果 
     */
    public function addInfo($hotelId, $roomTypeName, $roomTypePrice, $roomData = null, $roomTypeCharityPrice = null) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        //检查客栈是否已过期
        $hotelLogic->checkHotelActive($hotelId);
        //检查客栈是否启用
        $hotelLogic->checkHotelStatus($hotelId);

        // 检查是否可以添加
        $this->checkCanOperate($hotelId, null, $roomTypeName, $roomData);

        $roomTypeModel = D('RoomType');
        $roomTypeModel->startTrans();
        $commit = true;

        // 添加房型
        $addResult = $roomTypeModel->addInfo($hotelId, $roomTypeName, $roomTypePrice, $roomTypeCharityPrice);
        if (empty($addResult)) {
            $commit = false;
        }

        // 添加房间
        if ($commit && !empty($roomData)) {
            $roomTypeId = $addResult;
            foreach ($roomData as &$room) {
                $room['roomfrom'] = $this->typeMap[$room['from']];
            }

            $roomInfoModel = D('RoomInfo');
            $addResult = $roomInfoModel->addInfo($hotelId, $roomTypeId, $roomData);

            if (empty($addResult)) {
                $commit = false;
            }
        }

        if ($commit) {
            $roomTypeModel->commit();
            return $roomTypeId;
        } else {
            $roomTypeModel->rollback();
            returnJson(500, 508, 'error');
        }
    }

    /**
     * 修改房型
     * @modify trainyao 2016-01-04     创建
     * @modify wenhui 2016-12-16     添加discount参数
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  string   $roomTypeName  房型名称
     * @param  string   $roomTypePrice 房型默认价格
     * @param  array   $roomData      房型对应房间数据
     * @param  double   $discount 房型折扣值
     * @param  string   $roomTypeCharityPrice 房型义卖价格
     * @param  string   $minusdiscount 立减优惠或按比例优惠值
     * @param  string   $minustype 立减优惠方式 0=没优惠 1=立减优惠金额 2=按比例优惠
     * @return mixin 修改结果
     */
    public function editInfo($hotelId, $roomTypeId, $roomTypeName, $roomTypePrice, $roomData, $discount = null, $roomTypeCharityPrice = null, $minusdiscount = null, $minustype = null) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        //检查客栈是否已过期
        $hotelLogic->checkHotelActive($hotelId);
        //检查客栈是否启用
        $hotelLogic->checkHotelStatus($hotelId);
        // 检查是否可以修改
        $roomInfo = $this->checkCanOperate($hotelId, $roomTypeId, $roomTypeName, $roomData);
        
        if (!is_null($discount)) {
            //打折后价格不能小于义卖价
            if ($roomInfo[$roomTypeId]['price'] * ($discount/10) <= $roomInfo[$roomTypeId]['charityprice']) {
                returnJson(403, 426);
            }
            // 打折后价格不能小于立减优惠金额
            $minPrice = $roomInfo[$roomTypeId]['price'] - $roomInfo[$roomTypeId]['charityprice'];
            if ($roomInfo[$roomTypeId]['minustype'] == 1 && $minPrice * ($discount/10) <= $roomInfo[$roomTypeId]['minusdiscount']) {
                returnJson(403, 311, 'minusdiscount error');
            }
        }
        /*if (!is_null($discount) && $roomInfo[$roomTypeId]['price'] * ($discount/10) <= $roomInfo[$roomTypeId]['charityprice']) {
            returnJson(403, 426);
        }*/

        $roomTypeModel = D('RoomType');
        $roomTypeModel->startTrans();
        $commit = true;
        // 修改房型信息
        if (!empty($roomTypeName) || !empty($roomTypePrice) || !is_null($discount) || !is_null($roomTypeCharityPrice) || !is_null($minusdiscount) || !is_null($minustype)) {
            $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
            $editResult = $roomTypeModel->editInfo($hotelId, $roomTypeId, $roomTypeName, $roomTypePrice, $discount, $roomTypeCharityPrice, $minusdiscount, $minustype);
            // 获取客栈默认货币符号
            $symbol = $this->getDefultCurrencyByHotel($hotelId);
            $lang = $this->priceLogMap[LANG_SET];
            if (!is_null($discount)) {
                priceChangeLog($hotelId, $roomTypeId, $lang['discount']. $discount, strtotime(date('Ymd')), '', DISTR_PRICE, '');
            } elseif ($roomTypeInfo['price'] !== $roomTypePrice) {
                $detail = $lang['price'] . $symbol . $roomTypeInfo['price'] . $lang['change'] . $symbol . $roomTypePrice;
                priceChangeLog($hotelId, $roomTypeId, $detail, strtotime(date('Ymd')), '', DEFAULT_PRICE, '');
            }
            if (empty($editResult)) {
                $commit = false;
            }
        }

        // 修改房型关联的房间信息
        if ($commit && !empty($roomData)) {
            $currentRoomInfo = $roomInfo[$roomTypeId]['room'];

            // 处理房间的增删改数据
            $currentRoomInfo = getArrayValueAsIndex($currentRoomInfo, 'id');
            $roomToAdd = array();
            $roomToDelete = array();
            $roomToEditData = array();

            //增,改
            foreach ($roomData as $id => &$roomItem) {
                if ($roomItem['id'] == 0) {
                    $roomItem['roomfrom'] = 0;
                    array_push($roomToAdd, $roomItem);
                    unset($roomData[$id]);
                } else {
                    if (!empty($currentRoomInfo[$roomItem['id']]) && ($roomItem['name'] != $currentRoomInfo[$roomItem['id']]['name'])) {
                        array_push($roomToEditData,
                                array(
                            'id' => $roomItem['id'],
                            'name' => $roomItem['name'],
                            'roomfrom' => $this->typeMap[$roomItem['from']],
                            'masterentity_id' => $roomItem['masterentity_id'],
                            'masterbuildinfo_id' => $roomItem['masterbuildinfo_id'],
                            'masterfloorinfo_id' => $roomItem['masterfloorinfo_id'],
                            'masterroominfo_id' => $roomItem['masterroominfo_id'],
                        ));
                    }
                }
            }
            $roomData = getArrayValueAsIndex($roomData, 'id');
            //删
            foreach ($currentRoomInfo as $id => $currentRoom) {
                if (empty($roomData[$id])) {
                    array_push($roomToDelete, $id);
                }
            }
            // 执行修改
            $roomInfoModel = D('RoomInfo');
            if (!empty($roomToAdd)) {
                $editResult = $roomInfoModel->addInfo($hotelId, $roomTypeId, $roomToAdd);
                if (empty($editResult)) {
                    $commit = false;
                }
            }
            if ($commit && !empty($roomToDelete)) {
                $roomLogic = A('Common/Room', 'Logic');
                $editResult = $roomLogic->deleteInfo($hotelId, $roomToDelete, 'inMultiTrans');
                if (empty($editResult)) {
                    $commit = false;
                }
            }
            if ($commit && !empty($roomToEditData)) {
                $roomSynModel = D('RoomSyn');
                foreach ($roomToEditData as $editData) {
                    $editResult = $roomInfoModel->editInfo($hotelId, $editData['id'], $editData['name'], $editData['roomfrom']);

                    if (empty($editResult)) {
                        $commit = false;
                        break;
                    }
                    if($editData['roomfrom'] != 0){
                        $tempEditResult = $roomSynModel->getInfoByRoomId($editData['id']);
                        if($tempEditResult){
                            $editResult = $roomSynModel->editInfo($editData['masterentity_id'], $editData['masterbuildinfo_id'], $editData['masterfloorinfo_id'], $editData['masterroominfo_id'], $hotelId, $editData['id']);
                        }else{
                            $editResult = $roomSynModel->addInfo($editData['masterentity_id'], $editData['masterbuildinfo_id'], $editData['masterfloorinfo_id'], $editData['masterroominfo_id'], $hotelId, $editData['id']);
                        }

                        if (empty($editResult)) {
                            $commit = false;
                            break;
                        }
                    }
                }
            }
        }

        if ($commit) {
            $roomTypeModel->commit();
            return true;
        } else {
            $roomTypeModel->rollback();
            returnJson(500, 506, 'error');
        }
    }

    /**
     * 检查传入id是否存在
     * @modify trainyao 2016-04-29  创建
     * @param  integer   $hotelId    客栈id
     * @param  array   $roomTypeId 房型ids
     * @return boolean               是否存在
     */
    public function checkExist($hotelId, $roomTypeId) {
        $roomTypeInfo = $this->getInfoByHotel($hotelId);
        $roomTypeIdInDb = array_column($roomTypeInfo, 'id');
        $roomTypeId = array_diff($roomTypeId, $roomTypeIdInDb);

        return empty($roomTypeId) ? true : false;
    }

    /**
     * 检查是否有房型权限
     * 方法不抛异常
     * @modify trainyao 2016-07-01  创建
     * @param  integer   $hotelId    客栈ID
     * @param  mixin   $roomTypeId 房型ID(s)
     * @return boolean               是否有权限
     */
    public function checkHasRight($hotelId, $roomTypeId) {
        $userId = getUserId();
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        if (!$adminAuthorityLogic->checkIsAdmin($userId, $hotelId)) {
            $accountLogic = A('Common/Account', 'Logic');
            $roomAuthority = $accountLogic->getAdminRoomAuthority($userId, $hotelId);

            // 获取有权限房间所在的房型
            $roomInfoModel = D('RoomInfo');
            $roomInfo = $roomInfoModel->getInfoByRoomType($hotelId, $roomTypeId);

            $roomInfo = fliterArrayByKey($roomInfo, 'id', $roomAuthority);
            return empty($roomInfo) ? false : true;
        }

        return true;
    }

    /**
     * 检查房型房间是否已存在
     * @modify trainyao 2016-01-11    创建
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId       房型id
     * @param  string   $roomTypeName 房型名称
     * @param  array   $roomData    房型的房间数据
     * @return  array   房型的房间数据 array(roomTypeId => array(typeInfos + room => array(roomInfos)))
     */
    private function checkCanOperate($hotelId, $roomTypeId, $roomTypeName, $roomData) {
        $roomInfo = $this->getInfoByHotel($hotelId);

        // 房型名称是否存在
        $dataCompare = array();
        foreach ($roomInfo as $roomType) {
            array_push($dataCompare, array(
                'id' => $roomType['id'],
                'name' => $roomType['name']
            ));
        }
        unset($roomType);
        $dataCompare = getArrayValueAsIndex($dataCompare, 'id');
        foreach ($dataCompare as $_roomTypeId => $roomType) {
            if ($roomType['name'] == $roomTypeName && (empty($roomTypeId) || $roomTypeId != $_roomTypeId)) {
                returnJson(403, 302);
            }
        }
        unset($roomType);

        // 房间名称是否存在
        $dataCompare = array();
        foreach ($roomInfo as $roomType) {
            array_push($dataCompare, array(
                'id' => $roomType['id'],
                'name' => array_column($roomType['room'], 'name')
            ));
        }
        unset($roomType);
        $dataCompare = getArrayValueAsIndex($dataCompare, 'id');
        $roomDataNames = array_column($roomData, 'name');
        foreach ($dataCompare as $_roomTypeId => $roomType) {
            $cross = array_intersect($roomType['name'], $roomDataNames);
            // 房型名称有交集且( 新建房型时 或者 修改房型时 )
            if (!empty($cross) && (empty($roomTypeId) || $_roomTypeId != $roomTypeId)) {
                returnJson(403, 303, implode(',', $cross));
            }
        }

        return $roomInfo;
    }


    /**
     * 获取客栈的房型数据
     * @modify trainyao 2016-01-04 创建
     * @param  integer   $hotelId   客栈id
     * @param  integer   $hotelId   客栈id
     * @return array                房型数据 [{id, name, price, hotelentity_id, room=>[{
     *                                           id, name, roomfrom, isdirty
     *                                        }, {}, ...]}, {}, .......]
     */
    public function getInfoByHotel($hotelId, $getDeleted = false) {
        return $this->__singleGet('_getInfoByHotel', array($hotelId, $getDeleted));
    }
    protected function _getInfoByHotel($hotelId, $getDeleted) {
        $roomTypeModel = D('RoomType');
        $roomTypes = $roomTypeModel->getInfoByHotel($hotelId, $getDeleted);

        if (empty($roomTypes)) {
            return array();
        }

        $roomTypeIds = array_column($roomTypes, 'id');
        $roomInfoModel = D('RoomInfo');
        $roomInfo = $roomInfoModel->getInfoByRoomType($hotelId, $roomTypeIds, $getDeleted);
        $hourRoomModel = D('HourRoom');
        $hourRoomInfo = $hourRoomModel->getInfosByHotel($hotelId);
        $hourRoomIds = array_column($hourRoomInfo, 'roominfo_id');
        $longRoomModel = D('LongRoom');
        $longRoomInfo = $longRoomModel->getInfosByHotel($hotelId);
        $longRoomIds = array_column($longRoomInfo, 'roominfo_id');
        foreach ($roomInfo as &$room) {
            $isHourRoom = in_array($room['id'], $hourRoomIds);
            $isLongRoom = in_array($room['id'], $longRoomIds);
            if ($isHourRoom && $isLongRoom) {
                $room['class'] = 'room-icon03';
            } elseif ($isLongRoom) {
                $room['class'] = 'room-icon02';
            } elseif ($isHourRoom) {
                $room['class'] = 'room-icon01';
            } else {
                $room['class'] = '';
            }
            if ($isHourRoom) {
                $room['ishourroom'] = 1;
            } else {
                $room['ishourroom'] = 0;
            }
            if ($isLongRoom) {
                $room['islongroom'] = 1;
            } else {
                $room['islongroom'] = 0;
            }
        }
        $roomTypes = getArrayValueAsIndex($roomTypes, 'id');

        foreach ($roomInfo as $roomInfoItem) {
            if (empty($roomTypes[$roomInfoItem['roomtype_id']]['room'])) {
                $roomTypes[$roomInfoItem['roomtype_id']]['room'] = array();
            }
            array_push($roomTypes[$roomInfoItem['roomtype_id']]['room'], $roomInfoItem);
        }

        return $roomTypes;
    }

    /**
     * 删除房型
     * @modify trainyao 2016-01-04  创建
     * @param  integer   $hotelId    客栈id
     * @param  integer   $roomTypeId 房型id
     * @return mixin 执行结果
     */
    public function deleteInfo($hotelId, $roomTypeId) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        //检查客栈是否已过期
        $hotelLogic->checkHotelActive($hotelId);
        //检查客栈是否启用
        $hotelLogic->checkHotelStatus($hotelId);

        $roomInfoModel = D('RoomInfo');

        $roomInfoModel->startTrans();
        $commit = true;

        // 取出所有相关房间id
        $roomInfo = $roomInfoModel->getInfoByRoomType($hotelId, $roomTypeId);
        // 删除房间
        if (!empty($roomInfo)) {
            $roomIds = array_column($roomInfo, 'id');
            $roomLogic = A('Common/Room', 'Logic');
            $deleteResult = $roomLogic->deleteInfo($hotelId, $roomIds, 'inMultiTrans');
            if (empty($deleteResult)) {
                $commit = false;
            }
            $roomSynModel = D('RoomSyn');
            foreach ($roomInfo as $oneRoom) {
                if ($oneRoom['roomfrom'] != 0 ) {
                    $deleteResult = $roomSynModel->deleteInfo($oneRoom['id']);
                    if (empty($deleteResult)) {
                        $commit = false;
                        break;
                    }
                }
            }
        }
        // 删除上架房型
        if ($commit) {
            $roomTypeDetailModel = D('RoomTypeDetail');
            $deleteResult = $roomTypeDetailModel->deleteInfoByRoomType($roomTypeId);
            if (empty($deleteResult)) {
                $commit = false;
            }
        }

        // 删除房型数据
        if ($commit) {
            $roomTypeModel = D('RoomType');
            $deleteResult = $roomTypeModel->deleteInfo($roomTypeId);
            if (empty($deleteResult)) {
                $commit = false;
            }
        }

        // 删除钟点房数据
        if ($commit) {
            $hourRoomModel = D('HourRoom');
            $deleteResult = $hourRoomModel->deleteInfoByTypeId($roomTypeId);
            if (empty($deleteResult)) {
                $commit = false;
            }
        }

        // 删除钟点房数据
        if ($commit) {
            $longRoomModel = D('LongRoom');
            $deleteResult = $longRoomModel->deleteInfoByTypeId($roomTypeId);
            if (empty($deleteResult)) {
                $commit = false;
            }
        }

        if ($commit) {
            $roomInfoModel->commit();
            return true;
        } else {
            $roomInfoModel->rollback();
            returnJson(500, 510, 'error');
        }
    }

     /**
     * 获取价格日历
     * @modify wenhui 2016-12-16     创建
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  boolean   $isBook    是否获取hotelRoomTypePrice表中数据
     * @return mixin 修改结果
     */
    public function getRoomTypeInfoById($roomTypeId) {
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        return $roomTypeInfo;
    }

    /**
     * 获取价格日历
     * @modify wenhui 2016-12-16     创建
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  boolean   $isBook    是否获取hotelRoomTypePrice表中数据
     * @return mixin 修改结果
     */
    public function getPriceList($hotelId, $roomTypeId, $isBook = false) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        //检查客栈是否已过期
        $hotelLogic->checkHotelActive($hotelId);
        //检查客栈是否启用
        $hotelLogic->checkHotelStatus($hotelId);

        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        $discount = ($roomTypeInfo['discount'])/10;

        if (empty($roomTypeInfo)) {
            returnJson(500, 504,null);
        }

        $startTime = strtotime(date('Y-m-1'));
        $fillDays = date('w', $startTime) - 1;
        if($fillDays==-1){
            $fillDays = 6;
        }
        $startTime = $startTime - $fillDays*86400;

        $endTime = strtotime(date('Y-m-1', strtotime('+6 month')))-86400;
        $fillDays = 7 - date('w', $endTime);
        if($fillDays==7){
            $fillDays = 0;
        }
        $endTime = $endTime + $fillDays*86400;

        // 房型打折之后的默认价格price
        $roomTypeInfo['afterDiscount'] = number_format((($roomTypeInfo['price']*1000)/1000) * $discount, 2, '.', '');

        // 设置折扣值时 不获取当天之后已设置的预定价格
        $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
        $hotelRoomTypePriceList = $hotelRoomTypePriceModel->getPriceList($roomTypeId, $startTime, $endTime, $isBook);

        if (!empty($hotelRoomTypePriceList)) {
            // hotelRoomTypePrice表中的price是预定价格  不用打折
            $hotelRoomTypePriceList = getArrayValueAsIndex($hotelRoomTypePriceList,'price_date');
        }

        // 从RoomTypePrice表中获取 已设置的门市价格 门市价格要打折计算
        $roomTypePriceModel = D('RoomTypePrice');
        $roomTypePriceList = $roomTypePriceModel->getPriceList($roomTypeId, $startTime, $endTime);

        // RoomTypePrice表中price 计算打折后的日历价格
        foreach ($roomTypePriceList as &$PriceList) {
            $currentTime = strtotime(date('Y-m-d'));
            if ($PriceList['price_date'] >= $currentTime) {
                $PriceList['price'] = number_format((($PriceList['price']*1000)/1000) * $discount, 2, '.', '');
            }
        }
        unset($PriceList);

        $roomTypePriceList = getArrayValueAsIndex($roomTypePriceList,'price_date');

        $hotelRoomTypePriceList = isset($hotelRoomTypePriceList) ? $hotelRoomTypePriceList : array();

        // 循环$roomTypePriceList去掉在$hotelRoomTypePriceList中存在的价格日历
        foreach ($roomTypePriceList as $roomTypeKey => $roomTypItem) {
            foreach ($hotelRoomTypePriceList as $hotelRoomTypeKey => $hotelRoomTypeItem) {
                if ($roomTypeKey == $hotelRoomTypeKey ) {
                    unset($roomTypePriceList[$roomTypeKey]);
                }
            }
            unset($hotelRoomTypeItem);
        }
        unset($roomTypItem);

        $price_list = array_merge($roomTypePriceList , $hotelRoomTypePriceList);
        if (!empty($price_list)) {
            $price_list = getArrayValueAsIndex($price_list,'price_date');
            $roomTypeInfo['price_list'] = $price_list;
        } else {
            $roomTypeInfo['price_list'] = array();
        }

        $roomTypeInfo['currentTime'] = $currentTime;

        return $roomTypeInfo;
    }

    /**
     * 获取价格日历
     * @modify wenhui 2016-12-16     创建
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId    房型id
     * @return mixin 修改结果
     */
    public function setPrice($hotelId, $roomTypeId, $date, $price) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        //检查客栈是否已过期
        $hotelLogic->checkHotelActive($hotelId);
        //检查客栈是否启用
        $hotelLogic->checkHotelStatus($hotelId);

        $startTime = strtotime(date('Y-m-1'));
        $fillDays = date('w', $startTime) - 1;
        if($fillDays==-1){
            $fillDays = 6;
        }
        $startTime = $startTime - $fillDays*86400;

        $endTime = strtotime(date('Y-m-1', strtotime('+6 month')))-86400;
        $fillDays = 7 - date('w', $endTime);
        if($fillDays==7){
            $fillDays = 0;
        }
        $endTime = $endTime + $fillDays*86400;

        if($date<$startTime || $date>$endTime){
            returnJson(403, 220, null);
        }

        $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
        $hotelRoomTypePriceInfo = $hotelRoomTypePriceModel->getInfoByRoomType($roomTypeId, $date);
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        //价格不能小于义卖价
        if ($price <= $roomTypeInfo['charityprice']) {
            returnJson(403, 426);
        }

        $hotelRoomTypePriceModel->setPrice($roomTypeId, $date, $price);
        // 获取客栈默认货币符号
        $symbol = $this->getDefultCurrencyByHotel($hotelId);
        $lang = $this->priceLogMap[LANG_SET];
        if($hotelRoomTypePriceInfo){
            $detail = $lang['price'].$symbol.$hotelRoomTypePriceInfo['price'].$lang['change'].$symbol.number_format($price, 2, '.', '');
        }else{
            $detail = $lang['set'].$symbol.number_format($price, 2, '.', '');
        }
        priceChangeLog($hotelId, $roomTypeId, $detail, $date, '', DISTR_PRICE, '');
    }

    /**
     * 获取预设价格
     * @add Paddy 2016-12-1 创建
     * @param integer $hotelId 客栈ID
     * @param integer $startTime 结束时间戳
     * @param integer $endTime 结束时间戳
     */
    public function getRoomPrice($hotelId, $startTime, $endTime) {
        $roomInfoModel = D('RoomInfo');
        $roomInfo = $roomInfoModel->getInfoByHotel($hotelId);

        // 补充房型信息
        $roomTypeIds = array_values(array_unique(array_column($roomInfo, 'roomtype_id')));
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeIds);
        $roomTypeInfo = count($roomTypeIds) == 1 ? array(
            $roomTypeInfo
        ) : $roomTypeInfo;
        $roomTypeInfo = getArrayValueAsIndex($roomTypeInfo, 'id');

        $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
        $roomTypePriceList = $hotelRoomTypePriceModel->getPriceList($roomTypeIds, $startTime, $endTime);
        $priceData = array();
        foreach ($roomTypePriceList as $priceItem) {
            $priceData[$priceItem['roomtype_id'] . '_' . $priceItem['price_date']] = $priceItem['price'];
        }

        $result = array();
        foreach ($roomInfo as $roomItem) {
            $typeInfo = $roomTypeInfo[$roomItem['roomtype_id']];
            $priceArray = array();
            for ($timestamp = $startTime; $timestamp <= $endTime; $timestamp += 86400) {
                if (isset($priceData[$typeInfo['id'] . '_' . $timestamp])) {
                    $priceArray[]=array(
                        'timestamp'=>$timestamp,
                        'price'=>$priceData[$typeInfo['id'] . '_' . $timestamp]
                    );
                } else {
                    $priceArray[]=array(
                        'timestamp'=>$timestamp,
                        'price'=>$typeInfo['price']
                    );
                }
            }
            $result[] = array('room_id'=>(int)$roomItem['id'],'prices'=>$priceArray);
        }
        return $result;
    }

    /**
     * 客栈默认货币
     * @modify wenhui 2018-02-05    创建
     */
    protected function getDefultCurrencyByHotel($hotel) {
        $currencyModel = D('Currency');
        $currencyHotelModel = D('CurrencyHotel');

        $bpCurrency = $currencyModel->getCurrencyInfo();
        $bpCurrency = getArrayValueAsIndex($bpCurrency, 'id');
        $defaultHotelCurrency = $currencyHotelModel->getDefaultCurrency($hotel);
        $defaultSymbol = isset($bpCurrency[$defaultHotelCurrency['currency_id']]['symbol']) ? $bpCurrency[$defaultHotelCurrency['currency_id']]['symbol'] : '¥';

        return $defaultSymbol;
    }

}
